package com.example.log

import com.example.log.impl.LogcatImpl
import com.example.log.impl.ServerImpl

class Logger {

    private var mTag: String? = null
    private var mLevel: LoggerLevel? = null
    private var mType: LoggerType? = null
    private var mIsDebug = true
    private var mUrl: String? = null

    private var logger: ILogger? = null

    private fun Logger(
        _tag: String,
        _level: LoggerLevel,
        _type: LoggerType,
        _isDebug: Boolean,
        _url: String
    ) {
        mTag = _tag
        mLevel = _level
        mType = _type
        mIsDebug = _isDebug
        mUrl = _url
        logger = getLogger(mTag, mLevel, mType, mIsDebug, mUrl)
    }

    private fun getLogger(
        mTag: String?,
        mLevel: LoggerLevel?,
        mType: LoggerType?,
        mIsDebug: Boolean,
        mUrl: String?
    ): ILogger? {
        var _logger: ILogger? = null
        when (mType) {
            LoggerType.EMAIL -> throw IllegalStateException("EMAIL方案未实现暂时无法使用")
            LoggerType.LOGCAT -> _logger = LogcatImpl()
            LoggerType.SERVER -> _logger = ServerImpl()
            LoggerType.LOCALFILE -> throw IllegalStateException("LocalFile方案未实现暂时无法使用")
            else -> _logger = LogcatImpl()
        }
        if (null != _logger) {
            _logger.setDebug(mIsDebug)
            _logger.setLoggerLevel(mLevel)
            _logger.setTarget(mUrl)
            _logger.setLogTag(mTag)
        }
        return _logger
    }

    fun v(tag: String?, log: String?) {
        logger!!.v(tag, log)
    }

    fun d(tag: String?, log: String?) {
        logger!!.d(tag, log)
    }

    fun i(tag: String?, log: String?) {
        logger!!.i(tag, log)
    }

    fun w(tag: String?, log: String?) {
        logger!!.w(tag, log)
    }

    fun e(tag: String?, log: String?) {
        logger!!.e(tag, log)
    }


    class Builder {
        private var Tag: String? = null
        private var level: LoggerLevel? = null
        private var loggerType: LoggerType? = null
        private var isDebug = true
        private var url: String? = null
        fun setTag(tag: String?): Builder {
            Tag = tag
            return this
        }

        fun setLevel(level: LoggerLevel?): Builder {
            this.level = level
            return this
        }

        fun setLoggerType(loggerType: LoggerType?): Builder {
            this.loggerType = loggerType
            return this
        }

        fun setDebug(debug: Boolean): Builder {
            isDebug = debug
            return this
        }

        fun setUrl(url: String?): Builder {
            this.url = url
            return this
        }

//        fun build(): Logger {
//            return Logger(Tag,level,loggerType,isDebug,url)
//        }
    }

}